package com.swing.JTable;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.Vector;

public class MyFrame extends JFrame {

    //Model: 负责数值
    DefaultTableModel tableModel = new DefaultTableModel();

    //View : 负责显示，创建JTable的时候指定一个Model;
    JTable table = new JTable(tableModel);

    public MyFrame(String title) {
        super(title);


        //Content Pane
        JPanel root = new JPanel();
        this.setContentPane(root);
        root.setLayout(new BorderLayout());

        //添加到主界面
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        table.setRowSelectionAllowed(true);  //整行选择
        root.add(scrollPane, BorderLayout.CENTER);

        //初始化设置，添加5列
        tableModel.addColumn("学号");
        tableModel.addColumn("姓名");
        tableModel.addColumn("性别");
        tableModel.addColumn("出生日期");
        tableModel.addColumn("手机号");

        //列设置:自定义绘制
        TableColumn column2 = table.getColumnModel().getColumn(2);
        column2.setCellRenderer(new SexColumnRenderer());


        //添加数据行
        Student stu = new Student();
        stu.id = "20180001";
        stu.name = "li";
        stu.sex = true;
        stu.cellphone = "3214241214";
        stu.birthday = "1998-2-2";
        addTableRow(stu);

        Student stu2 = new Student();
        stu2.id = "20180002";
        stu2.name = "wang";
        stu2.sex = false;
        stu2.cellphone = "1472324124";
        stu2.birthday = "1998-2-2";
        addTableRow(stu2);


    }

    private void addTableRow(Student student) {
        //java.util.Vector 是个泛型，表示数组
        Vector<Object> rowData = new Vector<>();
        rowData.add(student.id);
        rowData.add(student.name);
        rowData.add(student.sex);
        rowData.add(student.birthday);
        rowData.add(student.cellphone);
        tableModel.addRow(rowData);

//        Object[] rowData = new Object[5];
//        rowData[0] = student.id;
//        rowData[1] = student.name;
//        rowData[2] = student.sex;
//        rowData[3] = student.birthday;
//        rowData[4] = student.cellphone;
//        tableModel.addRow(rowData);
    }

    // 负责 "性别" 这一列的显示
    static class SexColumnRenderer extends JLabel implements TableCellRenderer
    {
        public SexColumnRenderer()
        {
            //设置剧中，设置字体
            this.setHorizontalAlignment(SwingConstants.CENTER);
            this.setFont(this.getFont().deriveFont(Font.PLAIN));
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
                                                       int row, int column)
        {
            Boolean sex = (Boolean)value;
            if(sex != null && sex == true)
                this.setText("男");
            else
                this.setText("女");

            // 背景设置
            this.setOpaque(true);

            if (isSelected) {
                this.setBackground(table.getSelectionBackground());
                this.setForeground(table.getSelectionForeground());

            } else {
                this.setBackground(table.getBackground());
                this.setForeground(table.getForeground());
            }

            return this;
        }

    }


}
